﻿using System;
using System.Collections.Generic;
using System.Text;

namespace deFramework.BusinessRules
{
    /// <summary>
    /// .
    /// </summary>
    public class CustomListTools : Base.BaseBusinessRule
    {

        #region "Constructor, private Variables"
		
        /// <summary>
        /// 
        /// </summary>
        /// <param name="aContext"></param>
		public CustomListTools(ref Common.PresentationContext aContext) : base(ref aContext)  {}  
 
 
		#endregion

	
	
	#region "Public Static Constants"
	
        /// <summary>
        /// 
        /// </summary>
	public static string kMultiLineText = "kMultiLine";

    /// <summary>
    /// 
    /// </summary>
    public static string kSingleLineText = "kSingleLineText";

    /// <summary>
    /// 
    /// </summary>
    public static string kBoolean = "kBoolean";

    /// <summary>
    /// 
    /// </summary>
    public static string kNumeric = "kNumeric"; 
	
	
	#endregion 
	

/// <summary>
/// Safely add a new column (also called field) to a Custom List in Sharepoint if its not already existing. 
/// </summary>
/// <param name="aList">Object referencing the Custom List that is to get a new field.</param>
/// <param name="aFieldName">Name of the new field.</param>
/// <param name="aType">Sharepoint type of the new field.</param>
/// <returns>If successful the string reference id for the new field is returned. In case of an exception an empty string will be returned.</returns>
/// <example>string sField = oTools.TryCreateListField(oList, "MyField", SPFieldType.Text); </example>
		public string TryCreateBasicListField(Microsoft.SharePoint.SPList aList, string aFieldName, Microsoft.SharePoint.SPFieldType aType)
		{
			string l_Return = String.Empty;

			try
			{
				l_Return = aList.Fields.Add(aFieldName, aType, false);
				//this.
                //this.mLog.StandardLog("CustomListTools.TryCreateBasicListField", "information", "Result: "+ l_Return);
			}
			catch(Exception ex)
			{
                this.CurrentException = ex; 
				l_Return = String.Empty;
                //this.mLog.StandardLog("CustomListTools.TryCreateBasicListField", "information", "Exception Message: "+ ex.Message);
			}
			return l_Return;
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aList"></param>
        /// <param name="aFieldName"></param>
        /// <returns></returns>
        public bool ExistsListField(Microsoft.SharePoint.SPList aList, string aFieldName)
        {
            bool l_Return = false;

            try
            {
                foreach (Microsoft.SharePoint.SPField fld in aList.Fields)
                    if (fld.Title == aFieldName)
                    {
                        l_Return = true;
                        break;  
                    }
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return l_Return;
        }
	

/// <summary>
/// Safely search for a Custom List in a Sharepoint site and to create the list IF its not existing.
/// </summary>
/// <param name="aSiteUrl">URL of the Sharepoint Site where you want to try and create a custom list (if it does not already exist)</param>
/// <param name="aListTitle">The title of the Custom List you search for or create if it does not exist</param>
/// <param name="aDefaultDescription">The initial description for the custom list as specified.</param>
/// <returns>Reference to the retrieved or created Custom List is returned. In case of an exception then NULL will be the result.</returns>
/// <remarks></remarks>
/// <example>SPList oList = oTools.TryCreateCustomList("http://sp.ice.gov/ofm", "MyList", "My Description"); </example>
		public Microsoft.SharePoint.SPList TryCreateCustomList(string aSiteUrl, string aListTitle, string aDefaultDescription)
		{
			Microsoft.SharePoint.SPList oList = null;
			try
			{
				Microsoft.SharePoint.SPSite oSite = new Microsoft.SharePoint.SPSite(aSiteUrl);
				Microsoft.SharePoint.SPWeb oWeb = oSite.OpenWeb();
				try
				{
					oList = oWeb.Lists[aListTitle];
				}
				catch
				{
					oList = null;
					try
					{
						Guid oNewGuid = oWeb.Lists.Add(aListTitle, aDefaultDescription, oWeb.ListTemplates["Custom List"]);
						oList = oWeb.Lists[oNewGuid];

					}
					catch   (Exception ex) 
					{
                        this.CurrentException = ex;
                        // do nothing if 2nd exception occurs
                        //this.mLog.StandardLog("CustomListTools.TryCreateCustomList", "information", "Exception Message: "+ ex.Message);
						oList = null;
					}
				}
			}
			catch    (Exception ex) 
			{
				// do nothing if 2nd exception occurs
                //this.mLog.StandardLog("CustomListTools.TryCreateCustomList", "information", "Exception Message: "+ ex.Message);
                this.CurrentException = ex;
                oList = null;
			}
			return oList;

 
        
        }


        /// <summary>
        /// Safely search for a Custom List in a Sharepoint site web and to create the list IF its not existing.
        /// </summary>
        /// <param name="oWeb">Object representing the Sharepoint SPWeb where you want to try and create a custom list (if it does not already exist)</param>
        /// <param name="aListTitle">The title of the Custom List you search for or create if it does not exist</param>
        /// <param name="aDefaultDescription">The initial description for the custom list as specified.</param>
        /// <returns>Reference to the retrieved or created Custom List is returned. In case of an exception then NULL will be the result.</returns>
        /// <remarks></remarks>
        /// <example>SPList oList = oTools.WebCustomListCreate(l_SPWeb, "MyList", "My Description"); </example>
        public Microsoft.SharePoint.SPList WebCustomListCreate(
		  Microsoft.SharePoint.SPWeb oWeb, string aListTitle, string aDefaultDescription)
		  {	
			Microsoft.SharePoint.SPList oList = null;
				try
				{
					oList = oWeb.Lists[aListTitle];
				}
				catch
				{
					oList = null;
					try
					{
						Guid oNewGuid = oWeb.Lists.Add(aListTitle, aDefaultDescription, oWeb.ListTemplates["Custom List"]);
						oList = oWeb.Lists[oNewGuid];

					}
					catch   (Exception ex) 
					{
                        this.CurrentException = ex;
                        // do nothing if 2nd exception occurs
                        //this.mLog.StandardLog("CustomListTools.TryCreateCustomList", "information", "Exception Message: "+ ex.Message);
						oList = null;
					}
				}
			
			return oList;
		  } 


        #region "Dead Code" 

	
		 
        #endregion 
		
	

	}
}


 